Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

402
Visualizações
How to retrieve a DIV element from [object HTMLDivElement]?

I am using local storage to store a div element. However in the end I want to retrieve that div element from local storage, but every time I do that, I get back the output: "[object HTMLDivElement]", which is a string

Is there any way to convert this received result into an HTML element, so that I can get the same div, that I stored at the beginning?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

A good idea would be to transform the DOM object into a pure HTML string and then save it to local storage.

Look at this for further info: Parse HTML String to DOM and convert it back to string

about 4 years ago · Juan Pablo Isaza Relatório

0

localStorage can only store strings. You can't store DOM nodes in it, JS will just go "yeah that doesn't work, let me convert it to a string first" and will use the generic "object type in brackets" string.

If you want to store an HTML element, don't store the element itself but store its outerHTML source code, which is a string.

// convert page element to string:
function storeElement(element, keyName) {
  localStorage.setItem(keyName, element.outerHTML);
}

// reconstitute the page element from string:
function retrieveElement(keyName, preserve=false) {
  const stored = localStorage.getItem(keyName);
  if (!preserve) localStorage.removeItem(keyName);
  const helper = document.createElement(`section`);
  helper.innerHTML = stored;
  return helper.childNodes[0];
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda